tr td { padding: 0.1in; text-align: center; } tr td pre { padding: 0.1in; text-align: left; }

The for Loop

Nobody likes to do monotonous tasks over and over again (especially not programmers). That's where the for loop comes in. You could write a program like this:

printf("1\n");
printf("2\n");
printf("3\n");
printf("4\n");
but that's no fun. The for allows the programmer step back and just say to the computer, "count from 1 to 10, and print each number."

D
int main()

{   
   for (int i=1; i<=10; i++)
   {
      printf("%d\n", i);
   }
   return 0;
}

QuickBASIC
DIM I%

FOR I% = 1 TO 10
   PRINT I%
NEXT I%
Output
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10


Return to Tutorial Overview